﻿using ICSharpCode.AvalonEdit.CodeCompletion;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Editing;
using ICSharpCode.AvalonEdit.Highlighting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HimVoiceStudio
{
    /// <summary>
    /// EditorContainer.xaml 的交互逻辑
    /// </summary>
    public partial class EditorContainer : UserControl
    {
        public EditorContainer()
        {
            InitializeComponent();
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            ICSharpCode.AvalonEdit.Search.SearchPanel.Install(textEditor);
            textEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinitionByExtension(".hvst");
            textEditor.TextArea.TextEntered += TextArea_TextEntered;
        }



        private void TextArea_TextEntered(object sender, TextCompositionEventArgs e)
        {
            if (e.Text == "=")
            {

                completionWindow = new CompletionWindow(textEditor.TextArea);
                var completionData = completionWindow.CompletionList.CompletionData;
                completionData.Add(new DelayAutoCompletion("=", "150ms"));
                completionData.Add(new DelayAutoCompletion("==", "320ms"));
                completionData.Add(new DelayAutoCompletion("===", "0.5s"));
                completionData.Add(new DelayAutoCompletion("====", "0.8s"));
                completionData.Add(new DelayAutoCompletion("=====", "1.2s"));
                completionData.Add(new DelayAutoCompletion("======", "1.5s"));
                completionData.Add(new DelayAutoCompletion("=======", "2s"));
                completionWindow.Show();

                completionWindow.Closed += (o, args) => completionWindow = null;
            }
        }

        private CompletionWindow completionWindow;

        class DelayAutoCompletion : ICompletionData
        {
            public DelayAutoCompletion(string eqs, string dur)
            {
                this.text = eqs;
                this.duration = dur;
            }

            private string text;
            private string duration;

            public ImageSource Image => null;

            public string Text => text.Length + "";

            public object Content => text.Length + ". " + Description;

            public object Description => "停顿 " + duration;

            public double Priority => 1000 - duration.Length;

            public void Complete(TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs)
            {
                var newseg = new TextSegment() { StartOffset = completionSegment.Offset - 1, EndOffset = completionSegment.EndOffset };
                if (newseg.StartOffset > 0 && textArea.Document.GetCharAt(newseg.StartOffset - 1) == '[')
                {
                    newseg = new TextSegment() { StartOffset = completionSegment.Offset - 2, EndOffset = completionSegment.EndOffset };
                }
                textArea.Document.Replace(newseg, "[=" + duration + "]");
            }
        }

        class KeywordAutoCompletion
        {

        }
    }
}
